import api from "../../utils/api"
import Utils from "../../utils/utils";

// index.ts
Page({
  data: {
    bannerList: [] as BannerModel[],
    courseList: [] as CourseModel[],
    major: false as boolean,//是否是大客户
  },
  onLoad() {
    this.getBannerList();
    this.getCourseList();
  },
  onShow() {
    this.getUserInfo();
  },
  // 获取轮播图
  getBannerList(): void {
    api.index.getBannerList().then(res => {
      if (res.data.code == "000") {
        let list = res.data.data;
        if (!list) {
          return;
        }
        this.setData({
          bannerList: list
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: 'error',
          duration: 2000
        })
        console.error(res.data.message);
      }
    })
  },
  // 获取课程视频
  getCourseList(): void {
    api.index.getCourseList().then((res) => {
      if (res.data.code == "000") {
        let list = res.data.data;
        if (!list) {
          return;
        }
        list.map(item => {
          item.duration = Utils.convertSeconds(item.duration as number).join(":")
        })
        this.setData({
          courseList: list
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: 'error',
          duration: 2000
        })
        console.error(res.data.message);
      }
    })
  },
  // 样本绑定
  toBind(): void {
    wx.navigateTo({
      url: "/pages/samplebinding/samplebinding?major=" + this.data.major
    })
  },
  // 呼叫物流
  toLogistics(): void {
    wx.navigateTo({
      url: "/pages/logistics/logistics"
    })
  },
  // 进度查询
  toCheckProgress(): void {
    wx.navigateTo({
      url: "/pages/progress/progress"
    })
  },
  async toReport(): Promise<void> {
    const res = await api.index.reportExplainEnabled()
    if (res.data.code == "000") {
      wx.navigateTo({
        url: "/pages/report/report?major=" + this.data.major + "&showReport=" + res.data.data
      })
    } else {
      wx.showToast({
        title: res.data.message as string,
        icon: 'error'
      })
    }
  },
  // 获取是否为大客户信息
  async getUserInfo(): Promise<void> {
    const res = await api.user.getProfile()
    if (res.data.code === "000") {
      if (res.data.data) {
        this.setData({
          major: res.data.data.major
        })
      }
    } else {
      wx.showToast({
        title: res.data.message as string,
        icon: "error"
      })
    }
  },
  // 判断是否开启报告解读
  async reportExplainEnabled(): Promise<void> {
  },
  playVideo(e: any) {
    let video = e.currentTarget.dataset.video;
    wx.previewMedia({
      sources: [{
        type: 'video',
        url: video
      }]
    })
  },
  onShareAppMessage() {

  },
  onShareTimeline() {

  }
})
